
package physical_spaces.beans;

import Entity.TempFile;
import java.io.Serializable;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import login.beans.ConectionBean;
import physical_spaces.business.PhysicalSpaceBusiness;
import physical_spaces.business.TempFileDataModel;

/**
 *
 * @author JuanFelipe
 */
@ManagedBean
@SessionScoped
public class AdministracionTempFileBean  implements Serializable{
    
    private List<TempFile> tempFields ;
    private static final long serialVersionUID = 408L;
    TempFileDataModel mediumTempFileModel;
    public TempFile selectedTempFile;
    private String nameTempFile;
    private String tempFileSizeOption;
    private String actualSize;
    private String newExtendedSize;
    
    //propiedades componentes
    private int index;
    private String disabled;
    private String disabledParent;
    private Connection conn;

    public AdministracionTempFileBean() {
        
        index = 0;
        disabled = "true";
        
        tempFileSizeOption = "K";
     
        FacesContext context = javax.faces.context.FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
        ConectionBean conectionBean =(ConectionBean) session.getAttribute("conectionBean");
        
        conn = conectionBean.currentConnection;
        
        tempFields = new ArrayList<>();
        chargeData();
        mediumTempFileModel = new TempFileDataModel(tempFields);

    }

    public List<TempFile> getTempFields() {
        return tempFields;
    }

    public void setTempFields(List<TempFile> tempFields) {
        this.tempFields = tempFields;
    }

    public TempFileDataModel getMediumTempFileModel() {
        return mediumTempFileModel;
    }

    public void setMediumTempFileModel(TempFileDataModel mediumTempFileModel) {
        this.mediumTempFileModel = mediumTempFileModel;
    }

    public TempFile getSelectedTempFile() {
        return selectedTempFile;
    }

    public void setSelectedTempFile(TempFile selectedTempFile) {
        this.selectedTempFile = selectedTempFile;
    }

    public String getNameTempFile() {
        return nameTempFile;
    }

    public void setNameTempFile(String nameTempFile) {
        this.nameTempFile = nameTempFile;
    }

    public String getTempFileSizeOption() {
        return tempFileSizeOption;
    }

    public void setTempFileSizeOption(String tempFileSizeOption) {
        this.tempFileSizeOption = tempFileSizeOption;
    }

    public String getActualSize() {
        return actualSize;
    }

    public void setActualSize(String actualSize) {
        this.actualSize = actualSize;
    }

    public String getNewExtendedSize() {
        return newExtendedSize;
    }

    public void setNewExtendedSize(String newExtendedSize) {
        this.newExtendedSize = newExtendedSize;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public String getDisabled() {
        return disabled;
    }

    public void setDisabled(String disabled) {
        this.disabled = disabled;
    }

    public String getDisabledParent() {
        return disabledParent;
    }

    public void setDisabledParent(String disabledParent) {
        this.disabledParent = disabledParent;
    }

    public Connection getConn() {
        return conn;
    }

    public void setConn(Connection conn) {
        this.conn = conn;
    }
    
    public void cancel(){
        chargeData();
        mediumTempFileModel = new TempFileDataModel(tempFields);
        index = 0;
        disabledParent = "false";
        disabled = "true";
    }
    
    public void changeTab(){
        FacesContext context = FacesContext.getCurrentInstance();  
        if(selectedTempFile == null)
            context.addMessage(null,new FacesMessage("Error ","Por favor seleccione un TempFile"));
        else{
            nameTempFile = selectedTempFile.getName();
            actualSize = selectedTempFile.getTamano();
            actualSize = actualSize + " Bytes";
            newExtendedSize = "";
            tempFileSizeOption = "K";
            disabledParent = "true";
            disabled = "false";
            index++;
        }    
    }
    
    public void deleteTempFile() {
        
        FacesContext context = FacesContext.getCurrentInstance();
        PhysicalSpaceBusiness physicalSpaceBusiness = new PhysicalSpaceBusiness(conn);
        if (selectedTempFile == null) {
            context.addMessage(null, new FacesMessage("Error ", "Por favor seleccione un TempFile"));
        } else {
            context.addMessage(null,new FacesMessage("Mensaje ",physicalSpaceBusiness.crearQueryEliminarTempFile(selectedTempFile.getTableSpace(),selectedTempFile.getName())));
            chargeData();
            mediumTempFileModel = new TempFileDataModel(tempFields);
        }
    }
    
    public void expandirTamano() {
    
        FacesContext context = FacesContext.getCurrentInstance();
        PhysicalSpaceBusiness physicalSpaceBusiness 
                = new PhysicalSpaceBusiness( conn );
        if (newExtendedSize.equals("")) {
            context.addMessage(null, new FacesMessage("Error ", "Por favor ingrese un tamaño"));
        } else {
            context.addMessage(null,new FacesMessage("Mensaje ",physicalSpaceBusiness.reSizeTempFile(nameTempFile,newExtendedSize,tempFileSizeOption)));
            chargeData();
            mediumTempFileModel = new TempFileDataModel(tempFields);
            for(TempFile data : mediumTempFileModel){
                if(data.getName().equals(nameTempFile)){
                    actualSize = data.getTamano();
                    actualSize = actualSize + " Bytes";
                }
            }
        }
        
    }
    

    private void chargeData() {
        PhysicalSpaceBusiness psb = new PhysicalSpaceBusiness( conn );
        tempFields = psb.selectTempFiles();
    }
    
    
    
}
